<template>
  <div class="container">
    <div class="warp">
        <div class="songimg"> 
            <div class="conimg" :style="{animationPlayState:animationPlayState}">
                <img :src="this.musicinfo.image">
            </div>
            <div class="download" @click="downloadmusic">
                <el-button type="button" class="downloadbutton">下载歌曲</el-button>
            </div>
        </div>

        <div class="muslyric">
            <div class="headr">
                <div class="title">{{ this.musicinfo.name }}</div>
                <div class="singer">{{this.musicinfo.singer}}</div>
            </div>

            <div class="conlyric">
                <div class="songlyric" ref="songlyric" 
                :style="{transform:`translateY(${this.traTop}px)`}">
                    <p :class="nowTime == index ? 'active' : ''"
                        ref="songly"
                        v-for="(item,index) in Lyric" :key="index">
                        {{ item }} 
                    </p>
                </div>
            </div>
        </div>

        <div class="comment">

        </div>
    </div>
  </div>
</template>

<script>
import { mapState,mapMutations } from 'vuex';
export default {
    data(){
        return{
            Lyric:'',
            nowTime:'',
            offset:31,
            traTop:'',
            animationPlayState:'paused'
        }
    },
   computed:{
    ...mapState('muslyric',['musicinfo','lyric']),
    ...mapState('music',['isPlay','CurTime']),
   },
   mounted(){
        this.changeLyric()
   },
   methods:{
        changeLyric(){
            console.log(this.musicinfo)
            let obj ={}
            let reg = /\[(.*?)](.*)/g
            this.lyric.replace(/\n(\n)*()*(\n)*\n/g,'\n').replace(reg,(a,b,c)=>{
                b = b.slice(0,5)
                obj[b] = c
            })
            console.log(obj)
            this.Lyric = obj
        },

        transTime(time){
            let minut =  Math.floor(time/60)
            minut<10 ? (minut = '0'+minut):minut
            let second = Math.floor(time%60)
            second<10 ? (second = '0'+second):second
            return `${minut}:${second}`
        },
        LyricMove(){
            let active = document.getElementsByClassName("active")[0]
            console.log(active)
            if(active == null){
                return
            }
            let index = this.$refs.songly.indexOf(active)
            console.log(index)
            // console.log(index)
            if(active){
                if(active.offsetTop > this.offset){
                    this.traTop= -index*31
                }
            }
        },
             downloadmusic(){
        try{

            window.open(this.$baseUrl + '/files/download?filename=1695990386409_周杰伦%20-%20稻香.mp3')
        }catch(error){
            console.log(error)
        }
            // console.log(res)
            // this.$message.error(res.msg)
        }
    },
    watch:{
        CurTime(){
            let time = this.transTime(this.CurTime)
            console.log(time)
            if(time in this.Lyric){
                this.nowTime = time
            }
            this.$nextTick(()=>{
                this.LyricMove()
            })
        },
        isPlay(){
            if(this.isPlay){
                this.animationPlayState = "running"
            }
            else{
                this.animationPlayState = "paused"
            }
        }
    },
    created(){
        if(this.isPlay){
            this.animationPlayState = "running"
        }
        else{
            this.animationPlayState = "paused"
        }
    }
}
</script>

<style scoped lang="less">
.container{
    margin-top: 90px;
    padding: 0 100px;
    height: 100vh;
}
.container .warp{
    height: 100%;
    display: flex;
    justify-content: space-between;
}
.songimg{
    position: relative;
    width: 40%;
    padding: auto;
    padding-top: 200px;
    // background-color: aqua;
    // background:rgba(255, 255, 255, 0.5)
}
.warp .songimg .conimg{
    padding: 40px;
    width: 200px;
    height: 200px;
    background: #2C3E50;
    border-radius: 50%;
    box-sizing: border-box;
    margin: auto auto;
    animation: zhuan 12s infinite linear;
    animation-play-state: paused;
}
.active{
    color: crimson !important;
    font-size: 20px !important;
}
.download{
    width: 100%;
    height: 60px;
    box-sizing: border-box;
    text-align: center;
    margin-top: 20px;
}
.downloadbutton{
    width: 200px; 
    height: 60px; 
    border-radius: 15px;
    background-color: chartreuse;
    font-size: 25px;
}
.songimg .conimg img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
}

@keyframes zhuan{
    0%{
         transform: rotate(0deg);
    }
    100%{
        transform: rotate(360deg);
    }
}

.muslyric{
    width: 60%;
    height: 100%;
    position: relative;
}
.muslyric .headr{
    top:40px;
    position: relative;
    height: 180px;
    margin-right: 200px;
    margin-bottom: 10px;
    text-align: center;
}
.muslyric .title{
    font-size: 40px;
    height: 60px;
    text-align: center;
    color: burlywood;
}
.muslyric .singer{
    font-size: 30px;
    height: 60px;
    text-align: center;
}
.conlyric{
    position: relative;
    overflow: auto;
    height: 380px;

}
.conlyric::-webkit-scrollbar{
    display: none;
}
.muslyric .songlyric{
    margin-right: 200px;
    text-align: center;
    transition: 0.5s;
    background-color: rgba(0,0,0,0.5);
}

.muslyric .songlyric p{
    font-size: 16px;
    height: 31px;
    line-height: 31px;
    overflow: hidden;
}

</style>